package business;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.ListIterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import orm.AORef;
import orm.ChildrenBehaviorPurpose;
import orm.CollectivityHomework;
import orm.CollectivityHomeworkInfo;
import orm.CollectivityTeachingPlan;
import orm.CollectivityWeekArrange;
import orm.IHomeworkDomain;
import orm.IHomeworkDomainInfo;
import orm.IndividualHomework;
import orm.IndividualTeachingPlan;
import orm.TeachingPlanBaseInfo;
import util.DBHelper;
import util.Utility;
import util.WebConstants;
import dao.AORefDAO;
import dao.ChildrenBehaviorPurposeDAO;
import dao.CollectivityHomeworkDAO;
import dao.CollectivityHomeworkInfoDAO;
import dao.CollectivityTeachingPlanDAO;
import dao.IHomeworkDomainDAO;
import dao.IHomeworkDomainInfoDAO;
import dao.IndividualHomeworkDAO;
import dao.IndividualTeachingPlanDAO;
import dao.StudentDAO;
import dao.TPIDMemoryDAO;
import dao.TeacherDAO;
import dao.TeachingPlanBaseInfoDAO;

public class CollectivityTeachingPlanServlet extends BaseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public CollectivityTeachingPlanServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String strFunc = request.getParameter("func");
		
		if (strFunc == null) {
			return;
		} else if (strFunc.equals("load")) {
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();
			String teacherName = teacher.getTname();
			
			if (request.getAttribute("tpid") == null) {
				StudentDAO studentDAO = new StudentDAO();
				TeacherDAO teacherDAO = new TeacherDAO();
				
				request.setAttribute("teacherName", teacherName);
				
				Hashtable<Integer, String> ht = null;
				try {
					
					ht = studentDAO.getStudentsByTeacherID(teacherID);
					
				} catch (SQLException e) {
					e.printStackTrace();
				}
				if(ht == null){
					ht = new Hashtable<Integer, String>();
				}
				
				request.setAttribute("Students", ht);
				request.getRequestDispatcher("../" + Utility.JSP_CollectivityTeachingPlan).forward(request, response);
			} else {
				
			}
		} else if (strFunc.equals("save")) {
			TeachingPlanBaseInfo tpbi = new TeachingPlanBaseInfo();
			CollectivityTeachingPlan ctp = new CollectivityTeachingPlan();
			ArrayList<ChildrenBehaviorPurpose> al = new ArrayList<ChildrenBehaviorPurpose>();
			CollectivityHomework chw = new CollectivityHomework();
			ArrayList<CollectivityHomeworkInfo> alCHWI = new ArrayList<CollectivityHomeworkInfo>();
			TeacherDAO teacherDAO = new TeacherDAO();
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();
			String teacherName = teacher.getTname();
			pickupFormData(tpbi, ctp, al, chw, request);
			
			tpbi.setTeacherID(teacherID);
			chw.setTeacherID(teacherID);
			getChildrenBehaviorForInsert(al, request);
			getCollectivityHomeworkInfo(alCHWI, request);
			insertData(tpbi, ctp, al, chw, alCHWI);
			
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			try {
				request.setAttribute("TPList", tpbiDAO.getTPByUserName(teacherID));
				
				ArrayList coll = new ArrayList();
				coll = tpbiDAO.getCollectTPByUserName(teacherID);
				if(coll != null){
				request.setAttribute("collectionTPList", tpbiDAO.getCollectTPByUserName(teacherID));
				}
				
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			
			myTeachingPlan(request, response);
			response.sendRedirect("CollectivityTeachingPlanFunction?func=toFavor");
			
//			request.getRequestDispatcher("../" + Utility.JSP_MyTeachingPlan).forward(request, response);
		} else if (strFunc.equals("edit")) {
			String strTPID = request.getParameter("txtTPID");
			
			TeachingPlanBaseInfo tpbi = new TeachingPlanBaseInfo();
			CollectivityTeachingPlan ctp = new CollectivityTeachingPlan();
			ArrayList<ChildrenBehaviorPurpose> al = new ArrayList<ChildrenBehaviorPurpose>();
			CollectivityHomework chw = new CollectivityHomework();
			ArrayList<CollectivityHomeworkInfo> alCHWI = new ArrayList<CollectivityHomeworkInfo>();
			
			pickupFormData(tpbi, ctp, al, chw, request);
			getCollectivityHomeworkInfo(alCHWI, request);
			getChildrenBehaviorFromEdit(al,request);
			updateData(tpbi, ctp, al, chw, alCHWI,strTPID);
			
com.		zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			try {
				request.setAttribute("TPList", tpbiDAO.getTPByUserName(teacherID));
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			myTeachingPlan(request, response);
			response.sendRedirect("CollectivityTeachingPlanFunction?func=toFavor");
//			request.getRequestDispatcher("../" + Utility.JSP_MyTeachingPlan).forward(request, response);
			
		} else if (strFunc.equals("favor")) {
			String strTPID = request.getParameter("txtTPID");
			TeachingPlanBaseInfo tpbi = new TeachingPlanBaseInfo();
			CollectivityTeachingPlan ctp = new CollectivityTeachingPlan();
			ArrayList<ChildrenBehaviorPurpose> al = new ArrayList<ChildrenBehaviorPurpose>();
			CollectivityHomework chw = new CollectivityHomework();
			ArrayList<CollectivityHomeworkInfo> alCHWI = new ArrayList<CollectivityHomeworkInfo>();
			TeacherDAO teacherDAO = new TeacherDAO();
			
			pickupFormData(tpbi, ctp, al, chw, request);
			
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();
			String teacherName = teacher.getTname();

			tpbi.setTeacherID(teacherID);
			chw.setTeacherID(teacherID);
			getCollectivityHomeworkInfo(alCHWI, request);
			getChildrenBehaviorFromEdit(al,request);
			favorData(tpbi, ctp, al, chw, alCHWI, strTPID);
			
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			try {
				request.setAttribute("TPList", tpbiDAO.getTPByUserName(teacherID));
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			myTeachingPlan(request, response);
			request.getRequestDispatcher("../" + Utility.JSP_MyTeachingPlan).forward(request, response);
		} else if (strFunc.equals("printTP")) {
			String strTPID = request.getParameter("txtTPID");
			
			TeachingPlanBaseInfo tpbi = new TeachingPlanBaseInfo();
			CollectivityTeachingPlan ctp = new CollectivityTeachingPlan();			
			ArrayList<ChildrenBehaviorPurpose> cbpArr = new ArrayList<ChildrenBehaviorPurpose>();
			ChildrenBehaviorPurposeDAO cbpDAO = new ChildrenBehaviorPurposeDAO();
			
			pickupFormDataPrint(tpbi, ctp, cbpArr , request);
			try {
				cbpArr = cbpDAO.getChildrenBehaviorPurposeArrayByTPID(strTPID);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			request.setAttribute(WebConstants.TeachingPlanBaseInfo, tpbi);
			request.setAttribute(WebConstants.CollectivityTeachingPlan , ctp);
			request.setAttribute(WebConstants.ChildrenBehaviorPurpose , cbpArr);
			
			request.getRequestDispatcher("../" + Utility.JSP_PrintTP).forward(request, response);
		} else if (strFunc.equals("printCTPHomework")) {
			String strTPID = request.getParameter("txtTPID");
			com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
			
			Integer teacherID = teacher.getTeacherId();
			String teacherName = teacher.getTname();
			
			request.setAttribute("teacherName", teacherName);
			for(int CHomeworkID = 1; CHomeworkID < 6; CHomeworkID++){
				
					int completeEntering = Integer.parseInt(request.getParameter("CompleteEntering"+CHomeworkID));
					ArrayList<String> alCompleteEntering = Utility.CONSTANT_COMPLETEENTERING;
					String CompleteEntering = null;
			       	for (int i = 0; i<alCompleteEntering.size(); i++) {
			       		if (i == completeEntering-1) {
			       			CompleteEntering = alCompleteEntering.get(i);
			       		}
			   		}
			       	request.setAttribute("completeEnteringPrint"+CHomeworkID, CompleteEntering);
				
			}
			TeachingPlanBaseInfo tpbi = new TeachingPlanBaseInfo();
			CollectivityTeachingPlan ctp = new CollectivityTeachingPlan();
			ArrayList<ChildrenBehaviorPurpose> al = new ArrayList<ChildrenBehaviorPurpose>();
			CollectivityHomework chw = new CollectivityHomework();
			ArrayList<CollectivityHomeworkInfo> alCHWI = new ArrayList<CollectivityHomeworkInfo>();
			
			pickupFormData(tpbi, ctp, al, chw, request);
			getCollectivityHomeworkInfo(alCHWI, request);
			
			
			request.setAttribute("TPBI", tpbi);
			request.setAttribute(WebConstants.CollectivityTeachingPlan , ctp);
			request.setAttribute("CHW", chw);
			request.setAttribute("CHWIArr" , alCHWI);
			
			request.getRequestDispatcher("../" + Utility.JSP_PrintCTPHomework).forward(request, response);
		} else if(strFunc.equals("toFavor")){
			myTeachingPlan(request, response);
			
			request.getRequestDispatcher("../" + Utility.JSP_MyTeachingPlan).forward(request, response);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	public void pickupFormData(TeachingPlanBaseInfo tpbi, CollectivityTeachingPlan ctp, 
			ArrayList<ChildrenBehaviorPurpose> cbpArr, CollectivityHomework chw,HttpServletRequest request) {
		tpbi.setTpMaterial(Utility.TEACHINGPLAN_MATERIAL_TEXT);
		tpbi.setTpProperty(Integer.parseInt(request.getParameter("rdoTPProp")));
		tpbi.setTpType(Utility.TEAHCINGPLAN_TYPE_COLLECTIVITY);
		com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
		
		Integer teacherID = teacher.getTeacherId();
		String teacherName = teacher.getTname();
		tpbi.setConstitutor(teacherName);
		
		String strDateTmp = request.getParameter("txtWroteDateYear");
		tpbi.setWroteDate(strDateTmp);
		strDateTmp = request.getParameter("txtExecutionDateYear");
		tpbi.setExecutionDate(strDateTmp);
		tpbi.setAppropriateAge(Integer.parseInt(request.getParameter("sltAppropriateAge")));
		String[] strArrApproStu = request.getParameterValues("cbAppropriateStudents");
		StringBuffer sb = new StringBuffer();
		if(strArrApproStu != null){
			for (int i = 0; i < strArrApproStu.length; i++) {
				sb.append(strArrApproStu[i]);
				if (i != strArrApproStu.length -1) {
					sb.append(",");
				}
			}
		}
		
		tpbi.setAppropriateStudents(sb.toString());
		tpbi.setActivityName(request.getParameter("txtActivityName"));
		tpbi.setMonthThemeName(request.getParameter("MonthThemeName"));
		tpbi.setWeekThemeName(request.getParameter("WeekThemeName"));
		tpbi.setActivityType(Integer.parseInt(request.getParameter("rdoActivityType")));
		tpbi.setShare(Utility.BOOLEAN_FALSE);
		tpbi.setGrade(0);
		tpbi.setEditingTimes(0);
		
		ctp.setAppropriateClass(Integer.parseInt(request.getParameter("sltAppropriateClass")));
		ctp.setDevelopmentDomain(Integer.parseInt(request.getParameter("sltDevelopmentDomain")));
		ctp.setUsageMaterial(request.getParameter("txtUsageMaterial"));
		ctp.setActivityPurpose(request.getParameter("taActivityPurpose"));
		ctp.setActivityPreparation(request.getParameter("taActivityPreparation"));
		ctp.setTeachingContent(request.getParameter("taTeachingContent"));
		ctp.setTeachingWay(Integer.parseInt(request.getParameter("sltTeachingWay")));
		ctp.setActivityTime(request.getParameter("txtActivityTime"));
		ctp.setChildrenNum(Integer.parseInt(request.getParameter("txtChildrenNum")));
		ctp.setChildrenAge(request.getParameter("txtChildrenAge"));
		ctp.setTeachersNum(Integer.parseInt(request.getParameter("txtTeachersNum")));
		ctp.setMajorTeacherAssignment(request.getParameter("taMajorTeacherAssignment"));
		ctp.setAssistantTeacherAssignment(request.getParameter("taAssistantTeacherAssignment"));
		ctp.setExistentKnowledge(request.getParameter("taExistentKnowledge"));
		ctp.setActivityStepReporting(request.getParameter("taActivityStepReporting"));
		ctp.setTeachingMaterial(request.getParameter("taTeachingMaterial"));
		ctp.setTeachingExperience(request.getParameter("taTeachingExperience"));
		
		chw.setClassName(request.getParameter("ClassName"));
		chw.setHomeworkDate(request.getParameter("HomeworkDate"));
		chw.setTeacherName(teacherName);

	}
	
	public void pickupFormDataPrint(TeachingPlanBaseInfo tpbi, CollectivityTeachingPlan ctp, 
			ArrayList<ChildrenBehaviorPurpose> cbpArr, HttpServletRequest request) {
		com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
		
		Integer teacherID = teacher.getTeacherId();
		String teacherName = teacher.getTname();
		tpbi.setTpMaterial(Utility.TEACHINGPLAN_MATERIAL_TEXT);
		tpbi.setTpProperty(Integer.parseInt(request.getParameter("rdoTPProp")));
		tpbi.setTpID(request.getParameter("txtTPID"));
		tpbi.setTpType(Utility.TEAHCINGPLAN_TYPE_COLLECTIVITY);
		tpbi.setConstitutor(teacherName);
		String strConstitutor = request.getParameter("txtConstitutor");
		request.setAttribute("strConstitutor", strConstitutor);
		
		String strDateTmp = request.getParameter("txtWroteDateYear")  + "-" + request.getParameter("txtWroteDateMonth") +
			"-" + request.getParameter("txtWroteDateDay");
		tpbi.setWroteDate(strDateTmp);
		strDateTmp = request.getParameter("txtExecutionDateYear") + "-" + request.getParameter("txtExecutionDateMonth") + 
			"-" + request.getParameter("txtExecutionDateDay");
		tpbi.setExecutionDate(strDateTmp);
		ArrayList<String> alAppropriateAge = Utility.CONSTANT_APPROPRIATEAGE;
		String strAppropriateAge = null;
		int intAppropriateAge = Integer.parseInt(request.getParameter("sltAppropriateAge"));
       	for (int i = 0; i<alAppropriateAge.size(); i++) {
       		if (i == intAppropriateAge-1) {
       			strAppropriateAge = alAppropriateAge.get(i);
       		}
   		}
       	request.setAttribute("strAppropriateAge", strAppropriateAge);
		
		String[] strArrApproStu = request.getParameterValues("cbAppropriateStudents");
		StringBuffer sb = new StringBuffer();
		StringBuffer sbName = new StringBuffer();
		StudentDAO studentDAO = new StudentDAO();
		
		

		Hashtable<Integer, String> ht = null;
		try {
			ht = studentDAO.getStudentsByTeacherID(teacherID);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(strArrApproStu != null){
			for (int i = 0; i < strArrApproStu.length; i++) {
				sb.append(strArrApproStu[i]);
				if (i != strArrApproStu.length -1) {
					sb.append(",");
				}
				
				if(strArrApproStu[i]!=null && !strArrApproStu[i].equals("")){
					
					sbName.append(ht.get(new Integer(strArrApproStu[i])));
				}
				
//				sbName.append(ht.get(strArrApproStu[i]) );
				if (i != strArrApproStu.length -1) {
					sbName.append(",");
				}
			}
		}
		
		
		tpbi.setAppropriateStudents(sb.toString());
		tpbi.setAppropriateStudentNames(sbName.toString());
		
		tpbi.setActivityName(request.getParameter("txtActivityName"));
		tpbi.setMonthThemeName(request.getParameter("MonthThemeName"));
		tpbi.setWeekThemeName(request.getParameter("WeekThemeName"));
		ArrayList<String> alActivityType = Utility.CONSTANT_ACTIVITYTYPE;
		String strActivityType = null;
		int intActivityType = Integer.parseInt(request.getParameter("rdoActivityType"));
       	for (int i = 0; i<alActivityType.size(); i++) {
       		if (i == intActivityType-1) {
       			strActivityType = alActivityType.get(i);
       		}
   		}
       	request.setAttribute("strActivityType", strActivityType);
		tpbi.setShare(Utility.BOOLEAN_FALSE);
		tpbi.setGrade(0);
		tpbi.setEditingTimes(0);
		
		ArrayList<String> alAppropriateClass = Utility.CONSTANT_APPROPRIATECLASS;
		String strAppropriateClass = null;
		int intAppropriateClass = Integer.parseInt(request.getParameter("sltAppropriateClass"));
       	for (int i = 0; i<alAppropriateClass.size(); i++) {
       		if (i == intAppropriateClass-1) {
       			strAppropriateClass = alAppropriateClass.get(i);
       		}
   		}
       	request.setAttribute("strAppropriateClass", strAppropriateClass);
		ArrayList<String> alDevelopmentDomain = Utility.CONSTANT_DEVELOPMENTDOMAIN;
		String strDevelopmentDomain = null;
		int intDevelopmentDomain = Integer.parseInt(request.getParameter("sltDevelopmentDomain"));
       	for (int i = 0; i<alDevelopmentDomain.size(); i++) {
       		if (i == intDevelopmentDomain-1) {
       			strDevelopmentDomain = alDevelopmentDomain.get(i);
       		}
   		}
       	request.setAttribute("strDevelopmentDomain", strDevelopmentDomain);
		ctp.setUsageMaterial(request.getParameter("txtUsageMaterial"));
		ctp.setActivityPurpose(request.getParameter("taActivityPurpose"));
		ctp.setActivityPreparation(request.getParameter("taActivityPreparation"));
		ctp.setTeachingContent(request.getParameter("taTeachingContent"));
		ArrayList<String> alTeachingWay = Utility.CONSTANT_TEACHINGWAY;
		String strTeachingWay = null;
		int intTeachingWay = Integer.parseInt(request.getParameter("sltTeachingWay"));
       	for (int i = 0; i<alTeachingWay.size(); i++) {
       		if (i == intTeachingWay-1) {
       			strTeachingWay = alTeachingWay.get(i);
       		}
   		}
       	request.setAttribute("strTeachingWay", strTeachingWay);
		ctp.setActivityTime(request.getParameter("txtActivityTime"));
		ctp.setChildrenNum(Integer.parseInt(request.getParameter("txtChildrenNum")));
		ctp.setChildrenAge(request.getParameter("txtChildrenAge"));
		ctp.setTeachersNum(Integer.parseInt(request.getParameter("txtTeachersNum")));
		ctp.setMajorTeacherAssignment(request.getParameter("taMajorTeacherAssignment"));
		ctp.setAssistantTeacherAssignment(request.getParameter("taAssistantTeacherAssignment"));
		ctp.setExistentKnowledge(request.getParameter("taExistentKnowledge"));
		ctp.setActivityStepReporting(request.getParameter("taActivityStepReporting"));
		ctp.setTeachingMaterial(request.getParameter("taTeachingMaterial"));
		ctp.setTeachingExperience(request.getParameter("taTeachingExperience"));
		
		String strChildrenBehaviorPurpose0 = request.getParameter(0+"");
		String strChildrenBehaviorPurpose1 = request.getParameter(1+"");
		String strChildrenBehaviorPurpose2 = request.getParameter(2+"");
		String strChildrenBehaviorPurpose3 = request.getParameter(3+"");
		String strChildrenBehaviorPurpose4 = request.getParameter(4+"");
		String strChildrenBehaviorPurpose5 = request.getParameter(5+"");
		String strChildrenBehaviorPurpose6 = request.getParameter(6+"");
		request.setAttribute("strChildrenBehaviorPurpose"+0, strChildrenBehaviorPurpose0);
		request.setAttribute("strChildrenBehaviorPurpose"+1, strChildrenBehaviorPurpose1);
		request.setAttribute("strChildrenBehaviorPurpose"+2, strChildrenBehaviorPurpose2);
		request.setAttribute("strChildrenBehaviorPurpose"+3, strChildrenBehaviorPurpose3);
		request.setAttribute("strChildrenBehaviorPurpose"+4, strChildrenBehaviorPurpose4);
		request.setAttribute("strChildrenBehaviorPurpose"+5, strChildrenBehaviorPurpose5);
		request.setAttribute("strChildrenBehaviorPurpose"+6, strChildrenBehaviorPurpose6);
		
	}
	
	public void getChildrenBehaviorFromEdit(ArrayList<ChildrenBehaviorPurpose> cbpArr, HttpServletRequest request){
		ChildrenBehaviorPurposeDAO cbpDAO = new ChildrenBehaviorPurposeDAO();
		ArrayList<ChildrenBehaviorPurpose> al = null;
		try {
			al = cbpDAO.getChildrenBehaviorPurposeArrayByTPID(request.getParameter("txtTPID").toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for(int i = 0; i < al.size(); i++){
			ChildrenBehaviorPurpose cbp = new ChildrenBehaviorPurpose();
			cbp.setItemID(i);
			cbp.setContent(request.getParameter(i+""));
			cbp.setResult(Integer.parseInt(request.getParameter("cbpResult"+i)));
			cbpArr.add(cbp);
		}
	}
	
	public void getChildrenBehaviorForInsert(ArrayList<ChildrenBehaviorPurpose> cbpArr, HttpServletRequest request){
		for(int i = 0; i < 7; i++){
			ChildrenBehaviorPurpose cbp = new ChildrenBehaviorPurpose();
			cbp.setItemID(i);
			if(request.getParameter(""+i) != null){
				cbp.setContent(request.getParameter(""+i));
				
				cbp.setResult(1);
				cbpArr.add(cbp);
			}else{
				break;
			}
			
		}
	}
	
	public void getCollectivityHomeworkInfo(ArrayList<CollectivityHomeworkInfo> chwiArr, HttpServletRequest request){
		for(int cHomeworkID = 1; cHomeworkID < 6; cHomeworkID++){
			CollectivityHomeworkInfo chwi = new CollectivityHomeworkInfo();
			chwi.setCHomeworkID(cHomeworkID);
			chwi.setTaskDefinition(request.getParameter("TaskDefinition" + cHomeworkID));
			chwi.setMaterial(request.getParameter("Material" + cHomeworkID));
			chwi.setGuidingMeans(request.getParameter("GuidingMeans" + cHomeworkID));
			chwi.setCompleteEntering(Integer.parseInt(request.getParameter("CompleteEntering" + cHomeworkID)));
			chwi.setRemark(request.getParameter("Remark" + cHomeworkID));
			chwiArr.add(chwi);
		}
	}

	public void insertData(TeachingPlanBaseInfo tpbi, CollectivityTeachingPlan ctp, 
			ArrayList<ChildrenBehaviorPurpose> cbpArr, CollectivityHomework chw, ArrayList<CollectivityHomeworkInfo> chwiArr) {
		Connection conn = DBHelper.getConnection();
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
			conn.setAutoCommit(false);
			
			TPIDMemoryDAO tpidMemDAO = new TPIDMemoryDAO();
			int nTpid = tpidMemDAO.increaseTPID(stmt);
			String strTpid = "ABCD11" + nTpid;
			
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			tpbi.setTpID(strTpid);
			tpbiDAO.insertTeachingPlanBaseInfo(stmt, tpbi);
			
			CollectivityTeachingPlanDAO ctpDAO = new CollectivityTeachingPlanDAO();
			ctp.setTpID(strTpid);
			ctpDAO.insertCTP(stmt, ctp);
			
			ChildrenBehaviorPurposeDAO cbpDAO = new ChildrenBehaviorPurposeDAO();
			cbpDAO.insertChildrenBehaviorPurpose(stmt, cbpArr, strTpid);
			
			CollectivityHomeworkDAO chwDAO = new CollectivityHomeworkDAO();
			chwDAO.insertchw(stmt, chw, strTpid);
			
			CollectivityHomeworkInfoDAO chwiDAO = new CollectivityHomeworkInfoDAO();
			chwiDAO.insertCHomeworkInfo(stmt, chwiArr, strTpid);
			
			conn.commit();
			
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException ee) {
				
			}
		}
	}

	public void updateData(TeachingPlanBaseInfo tpbi, CollectivityTeachingPlan ctp,
			ArrayList<ChildrenBehaviorPurpose> cbpArr, CollectivityHomework chw,
			ArrayList<CollectivityHomeworkInfo> chwiArr, String tpid) {
		Connection conn = DBHelper.getConnection();
		Statement stmt = null;
		
		try {
			conn.setAutoCommit(false);
			stmt = conn.createStatement();
			
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			tpbi.setTpID(tpid);
			tpbiDAO.updateTPByTPID(stmt, tpbi);
			
			CollectivityTeachingPlanDAO ctpDAO = new CollectivityTeachingPlanDAO();
			ctp.setTpID(tpid);
			ctpDAO.updateCTP(stmt, ctp);
			
			ChildrenBehaviorPurposeDAO cbpDAO = new ChildrenBehaviorPurposeDAO();
			cbpDAO.updateChildrenBehaviorPurpose(stmt, cbpArr, tpid);
			
			CollectivityHomeworkDAO chwDAO = new CollectivityHomeworkDAO();
			chw.setTPID(tpid);
			chwDAO.updatechw(stmt, chw, tpid);
			
			CollectivityHomeworkInfoDAO chwiDAO = new CollectivityHomeworkInfoDAO();
			chwiDAO.updateCHomeworkInfo(stmt, chwiArr, tpid);
			
			conn.commit();
			
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException ee) {
				
			}
		}
	}

	public void favorData(TeachingPlanBaseInfo tpbi, CollectivityTeachingPlan ctp, 
			ArrayList<ChildrenBehaviorPurpose> cbpArr, CollectivityHomework chw,
			ArrayList<CollectivityHomeworkInfo> chwiArr, String tpid) {
		Connection conn = DBHelper.getConnection();
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
			conn.setAutoCommit(false);
			
			TPIDMemoryDAO tpidMemDAO = new TPIDMemoryDAO();
			int nTpid = tpidMemDAO.increaseTPID(stmt);
			String strTpid = tpid + Utility.TPID_CONNNECTSTRING + "ABCD11" + nTpid;
			
			TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
			tpbi.setTpID(strTpid);
			tpbiDAO.insertTeachingPlanBaseInfo(stmt, tpbi);
			
			CollectivityTeachingPlanDAO ctpDAO = new CollectivityTeachingPlanDAO();
			ctp.setTpID(strTpid);
			ctpDAO.insertCTP(stmt, ctp);
			
			ChildrenBehaviorPurposeDAO cbpDAO = new ChildrenBehaviorPurposeDAO();
			cbpDAO.insertChildrenBehaviorPurpose(stmt, cbpArr, strTpid);
			
			CollectivityHomeworkDAO chwDAO = new CollectivityHomeworkDAO();
			chw.setTPID(strTpid);
			chwDAO.insertchw(stmt, chw, strTpid);
			
			CollectivityHomeworkInfoDAO chwiDAO = new CollectivityHomeworkInfoDAO();
			chwiDAO.insertCHomeworkInfo(stmt, chwiArr, strTpid);
			
			conn.commit();
			
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException ee) {
				
			}
		}
	}
	public void myTeachingPlan(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException{
		TeachingPlanBaseInfoDAO tpbiDAO = new TeachingPlanBaseInfoDAO();
		com.zmht.specedu.vo.Teacher teacher = this.getCurrentUser(request); 
		
		Integer teacherID = teacher.getTeacherId();
		String teacherName = teacher.getTname();

		try {
			request.setAttribute("TPList", tpbiDAO.getTPByUserName(teacherID));
			ArrayList coll = new ArrayList();
			coll = tpbiDAO.getCollectTPByUserName(teacherID);
			if(coll != null){
			request.setAttribute("collectionTPList", tpbiDAO.getCollectTPByUserName(teacherID));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		TeachingPlanBaseInfo tpbi = null;

		try {
			request.setAttribute("pageNo", 1);
			int PageSize = Utility.PageSize;
			ArrayList al = tpbiDAO.getTPByUserName(teacherID);
			int totalPage = 0;
			if (al.size() % PageSize == 0) {
				totalPage = al.size()/PageSize;
				request.setAttribute("totalPage",totalPage);
			} else {
				totalPage = (al.size()/PageSize) + 1;
				request.setAttribute("totalPage",totalPage);
			}
			
			al = new ArrayList<TeachingPlanBaseInfo>();
			ListIterator<TeachingPlanBaseInfo> it = tpbiDAO.getTPByUserName(teacherID).listIterator();
			int ii = 0;
			while (it.hasNext()) {
				TeachingPlanBaseInfo t = it.next();
				tpbi = new TeachingPlanBaseInfo();
				tpbi.setActivityName(t.getActivityName());
				tpbi.setEditingTimes(t.getEditingTimes());
				tpbi.setTeacherName(t.getTeacherName());
				tpbi.setTpType(t.getTpType());
				tpbi.setTpID(t.getTpID());
				al.add(tpbi);
				ii++;

				if(ii > (PageSize-1) ) {
					break;
				}
			}
			
			request.setAttribute("TPList", al);
			
			ArrayList coll = new ArrayList();
			coll = tpbiDAO.getCollectTPByUserName(teacherID);
			if(coll != null){
			request.setAttribute("collectionTPList", tpbiDAO.getCollectTPByUserName(teacherID));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
